package com.xiaozeng.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaozeng.category.mapper.CategoryMapper;
import com.xiaozeng.category.service.CategoryService;
import com.xiaozeng.param.ProductHotParam;
import com.xiaozeng.pojo.Category;
import com.xiaozeng.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: xiaozeng
 * @Date: 2022 11
 * @Description:
 **/
@Service
@Slf4j
public class CategoryImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public Result queryByName(String categoryName) {

        //数据库查询
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name",categoryName);
        Category category = categoryMapper.selectOne(queryWrapper);
        if (category == null){
            log.info("CategoryServiceImpl.queryByName业务结束，结果:{}","类别查询失败");
            return Result.fail("类别查询失败");
        }
        //返回对象
        log.info("CategoryServiceImpl.detail业务结束，结果:{}",category);
        return Result.ok("类别查询成功",category);
    }

    @Override
    public Result hotsCategory(ProductHotParam productHotParam) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("category_name",productHotParam.getCategoryName());
        queryWrapper.select("category_id");

        List<Object> ids = categoryMapper.selectObjs(queryWrapper);

        return Result.ok("类别集合查询",ids);
    }

    @Override
    public Result list() {
        List<Category> categories = categoryMapper.selectList(null);

        //最多返回12条数据
        List<Category> list = categories.stream().limit(Math.min(categories.size(), 12)).collect(Collectors.toList());

        log.info("CategoryServiceImpl.list业务结束，结果:{}",list);
        return Result.ok("成功",list);
    }
}
